package com.example.planetexploder.model.folder;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.example.planetexploder.bean.maindata.ImgInfo;
import com.example.planetexploder.constants.LogConst;
import com.example.planetexploder.model.dao.Dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import static com.example.planetexploder.constants.FolderConst.IMAGE_FOLDER_NAME;

/**
 * 图片文件操作类
 * @author 蒋子涵
 * 2019.10.15
 */
public class ImgFile {

    private File root;
    private Context context;

    public ImgFile(Context context, File root){
        this.context = context;
        this.root = root;
    }

    /**
     * 创建图片组文件夹
     * @param bookId        摘抄集id
     * @param imgGroupId  图片组id
     */
    public void create_ImgGroupFolder(String bookId, String imgGroupId) {
        Log.d(LogConst.MTAG, "create_ImgGroupFolder()");
        File file = new File(root,bookId+"/"+IMAGE_FOLDER_NAME + "/"+imgGroupId);
        if(!file.exists())
            file.mkdir();
    }

    /**
     * 删除图片组文件夹
     * @param bookId        摘抄集id
     * @param imgGroupId  图片组id
     */
    public void delete_ImgGroupFolder(String bookId, String imgGroupId) {
        Log.d(LogConst.MTAG, "delete_ImgGroupFolder()");
        File file = new File(root,bookId+"/"+IMAGE_FOLDER_NAME + "/"+imgGroupId);
        if(file.exists())
        {
            File[] items = file.listFiles();
            if(items != null)
                for (File f : items)
                    f.delete();
            file.delete();
        }
    }

    /**
     * 向图片组文件夹中添加图片
     * @param bookId        摘抄集id
     * @param imgGroupId  图片组id
     * @param imageId       图片id
     * @param imgSource     图片文件
     */
    public void add_Image_To_ImgGroupFolder(String bookId, String imgGroupId, String imageId, Bitmap imgSource) {
        Log.d(LogConst.MTAG, "add_Image_To_ImgGroupFolder()");
        File imageFile = new File(root,bookId + "/" + IMAGE_FOLDER_NAME + "/" + imgGroupId + "/" + imageId + ".jpg");
        if(!imageFile.exists()) {
            try {
                imageFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(imageFile);
            if(imgSource!=null)
                if(imgSource.compress(Bitmap.CompressFormat.JPEG,100,fileOutputStream))
                {
                    fileOutputStream.flush();
                    fileOutputStream.close();
                }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 从图片组文件夹中删除图片
     * @param bookId        摘抄集id
     * @param imgGroupId  图片组id
     * @param imgId       图片id
     */
    public void delete_Image_From_ImgGroupFolder(String bookId, String imgGroupId, String imgId) {
        Log.d(LogConst.MTAG, "delete_Image_From_ImgGroupFolder()");
        File file = new File(root,bookId+"/"+IMAGE_FOLDER_NAME + "/"+imgGroupId+"/"+imgId+".jpg");
        if(file.exists())
            file.delete();
    }

    /**
     * 获取图片组中的所有图片
     * @param bookId        摘抄集id
     * @param imgGroupId  图片组id
     * @return              图片组所有图片的文件
     */
    public ArrayList<Bitmap> get_ImageGroup(String bookId, String imgGroupId) {
        Log.d(LogConst.MTAG, "get_ImgGroup()");
        File file;
        ArrayList<Bitmap> imgFiles = new ArrayList<Bitmap>();
        Dao dao = new Dao(context);
        FileInputStream fis;
        Bitmap bitmap;
        ArrayList<ImgInfo> imageData = dao.imgGroupTable.get_ImgGroupTable(imgGroupId);
        for (ImgInfo imgInfo:imageData)
        {
            file =new  File(root,bookId + "/" + IMAGE_FOLDER_NAME + "/" + imgGroupId + "/" + imgInfo.imgId + ".jpg");
            try {
                fis = new FileInputStream(file.getAbsoluteFile());
                bitmap = BitmapFactory.decodeStream(fis);
                imgFiles.add(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        return imgFiles;
    }

    /**
     * 获取图片文件
     * @param bookId            摘抄集id
     * @param imgGroupId      图片组id
     * @param imgId           图片id
     * @return                  图片文件
     */
    public File get_Image(String bookId, String imgGroupId, String imgId) {
        Log.d(LogConst.MTAG, "get_Image()");
        return new File(root,bookId + "/" + IMAGE_FOLDER_NAME + "/" + imgGroupId + "/" + imgId + ".jpg");
    }
}
